//
//  ViewController.m
//  JuLinkLable
//
//  Created by Juvid on 16/9/29.
//  Copyright © 2016年 Juvid. All rights reserved.
//

#import "ViewController.h"
#import "JuLinkLabel.h"
#import "JuLayoutFrame.h"
@interface ViewController (){
    
    __weak IBOutlet JuLinkLabel *sh_Lable;
}
@property(nonatomic, strong) NSArray* matches;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//     sh_Lable.text=@"This is link num is 18516285756 text with a link www.baidu.com and another link http://www.zol.com.cn and link http://www.pconline.com.cn and finally google.com";

    JuLinkLabel *labTest=[[JuLinkLabel alloc]init];
    [self.view addSubview:labTest];
    labTest.numberOfLines=0;
    labTest.juTopSpace.toView(sh_Lable).equal(10);
    labTest.juLead.equal(10);
    labTest.juTrail.equal(10);
//    labTest.text=@"Test link num is 18516285756 text with a link www.baidu.com and another link http://www.zol.com.cn and link http://www.pconline.com.cn and finally google.com";
//    NSError *error = NULL;
//    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber error:&error];
//
//    self.matches = [detector matchesInString:sh_Lable.text options:0 range:NSMakeRange(0, sh_Lable.text.length)];
//     [self highlightLinksWithIndex:NSNotFound];
    // Do any additional setup after loading the view, typically from a nib.
}
#pragma mark -

- (BOOL)isIndex:(CFIndex)index inRange:(NSRange)range {
    return index > range.location && index < range.location+range.length;
}
- (void)highlightLinksWithIndex:(CFIndex)index {

    NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc]initWithString:sh_Lable.text];

    for (NSTextCheckingResult *match in self.matches) {

        if ([match resultType] == NSTextCheckingTypeLink) {

            NSRange matchRange = [match range];

            if ([self isIndex:index inRange:matchRange]) {
                [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:matchRange];
            }
            else {
                [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:matchRange];
            }

            [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:matchRange];
        }if ([match resultType] == NSTextCheckingTypePhoneNumber) {
            NSRange matchRange = [match range];
            [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:matchRange];
        }
    }

//    self.label.attributedText = attributedString;

    sh_Lable.attributedText=attributedString;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
